package com.sunfeng.chap2;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 验证 int read(ByteBuffer dst) 缓冲区remaining为多少，就从通道中读取多少数据
 */
public class Test2_6 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(new File("d:\\a.txt"));
        FileChannel fileChannel = fileInputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        byteBuffer.position(1);
        byteBuffer.limit(3);

        fileChannel.read(byteBuffer);
        fileChannel.close();
        fileInputStream.close();
        byteBuffer.rewind();


        for (int i = 0; i < byteBuffer.limit(); i++) {
            byte b = byteBuffer.get();
            if(b==0){
                System.out.print("空格 ");
            }else{
                System.out.print((char) b+" ");
            }

        }
    }
}
